package com.iot.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.SysCode;
import com.iot.manage.result.Code;
import com.iot.manage.result.Result;
import com.iot.manage.service.SysCodeService;
import com.iot.manage.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统编码管理控制层
 *
 * @author zhuzhi
 */
@RestController
@RequestMapping("/api/private/v1")
public class SysCodeController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysCodeService sysCodeService;

    /**
     * 查询系统编码信息列表
     *
     * @param token
     * @param query 模糊查询关键字
     * @param pagenum 当前页
     * @param pagesize 每页条数
     * @return
     */
    @GetMapping("/findSysCodeList")
    public Result findSysCodeList(
            @RequestHeader("token") String token,
            @RequestParam("query") String query,
            @RequestParam("pagenum") int pagenum,
            @RequestParam("pagesize") int pagesize) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<SysCode> sysCodeList;
        int sysCodeTotal;
        try{
            sysCodeList = sysCodeService.findSysCodeList(query,pagenum,pagesize);
            sysCodeTotal = sysCodeService.countSysCodeList(query);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",sysCodeTotal);
        map.put("pagenum",pagenum);
        map.put("sysCodeList",sysCodeList);
        return result.build(map, Code.S200);
    }

    /**
     * 新增系统编码信息
     *
     * @param token
     * @param params 系统编码信息
     * @return
     */
    @PostMapping("/addSysCodeInfo")
    public Result addSysCodeInfo(
            @RequestHeader("token") String token,
            @RequestBody JSONObject params) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            sysCodeService.addSysCodeInfo(params);
        } catch (DuplicateKeyException e){
            return result.build(null, Code.D413);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 根据系统编码英文名查询系统编码信息
     *
     * @param token
     * @param sysCodeType 系统编码分类
     * @param codeName 系统编码名
     * @return
     */
    @GetMapping("/findSysCodeInfoBySysCodeTypeAndCodeName/{sysCodeType}/{codeName}")
    public Result findSysCodeInfoBySysCodeTypeAndCodeName(
            @RequestHeader("token") String token,
            @PathVariable("sysCodeType") String sysCodeType,
            @PathVariable("codeName") String codeName){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        SysCode sysCode;
        try{
            sysCode = sysCodeService.findSysCodeInfoBySysCodeTypeAndCodeName(sysCodeType,codeName);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(sysCode, Code.S200);
    }

    /**
     * 修改系统编码信息
     *
     * @param token
     * @param params 系统编码信息参数
     * @return
     */
    @PutMapping("updateSysCodeInfo/{oldSysCodeType}/{oldSysCodeName}")
    public Result updateSysCodeInfo(
            @RequestHeader("token") String token,
            @PathVariable("oldSysCodeType") String oldSysCodeType,
            @PathVariable("oldSysCodeName") String oldSysCodeName,
            @RequestBody JSONObject params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            params.put("oldSysCodeType",oldSysCodeType);
            params.put("oldSysCodeName",oldSysCodeName);
            sysCodeService.updateSysCodeInfo(params);
        } catch (DuplicateKeyException e){
            return result.build(null, Code.D413);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     *删除系统编码信息
     *
     * @param token
     * @param sysCodeType 系统编码分类
     * @param codeName 系统编码名
     * @return
     */
    @DeleteMapping("/removeSysCodeInfo/{sysCodeType}/{codeName}")
    public Result removeSysCodeInfo(
            @RequestHeader("token") String token,
            @PathVariable("sysCodeType") String sysCodeType,
            @PathVariable("codeName") String codeName) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            sysCodeService.removeSysCodeInfo(sysCodeType,codeName);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }
}
